package com.kuang.blog.test;
import java.util.Random;
import java.util.Scanner;

public class GuessNumberGame {
    public static void main(String[] args) {
        Random random = new Random();
        int numberToGuess = random.nextInt(100) + 1; // 生成1到100之间的随机整数

        Scanner scanner = new Scanner(System.in);
        int guess;
        int attempts = 0;
        boolean isCorrect = false;

        System.out.println("欢迎来到猜数字游戏！");
        System.out.println("系统已生成一个1到100之间的随机整数，请开始猜测吧！");

        while (!isCorrect) {
            System.out.print("请输入你的猜测：");
            guess = scanner.nextInt();
            attempts++;

            if (guess == numberToGuess) {
                System.out.println("恭喜你，猜对了！正确答案是 " + numberToGuess);
                System.out.println("你共猜测了 " + attempts + " 次。");
                isCorrect = true;
            } else if (guess < numberToGuess) {
                System.out.println("猜错了！猜的数字小了，请继续猜测。");
            } else {
                System.out.println("猜错了！猜的数字大了，请继续猜测。");
            }
        }

        scanner.close();
    }
}